#pragma once
#include "common.hpp"
#include "InetAddr.hpp"
#include <memory>

namespace SocketModule
{
    using namespace LogModule;

    class Socket
    {
    public:
        virtual void SocketOrDie() = 0;
        virtual void BindOrDie(uint16_t port) = 0;
        virtual void ListenOrDie(int backlog) = 0;
        virtual std::shared_ptr<Socket> Accept(InetAddr* Client) = 0;
        virtual bool Connect(std::string& ip, uint16_t port) = 0;
        virtual void Close() = 0;
        virtual int Recv(std::string* out) = 0;
        virtual int Send(const std::string& message) = 0;

    public:
        void BuildTcpSocketMethod(int port, int backlog = 16)
        {
            SocketOrDie();
            BindOrDie(port);
            ListenOrDie(backlog);
        }
        void BulidClientSocketMethod()
        {
            SocketOrDie();
        }
    };

    const int DefaultNum = -1;

    class TcpSocket : public Socket
    {
    public:
        TcpSocket()
            : _sockfd(DefaultNum)
        {
        }
        TcpSocket(int fd)
            : _sockfd(fd)
        {
        }

        void SocketOrDie() override
        {
            _sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
            if (_sockfd < 0)
            {
                LOG(LogLevel::FATAL) << "socket error";
                exit(SOCKET_ERROR);
            }
            LOG(LogLevel::INFO) << "socket sunccess";
        }
        void BindOrDie(uint16_t port) override
        {
            InetAddr localAddr(port);
            int n = ::bind(_sockfd, localAddr.NetAddrptr(), localAddr.NetAddrLen());
            if (n < 0)
            {
                LOG(LogLevel::FATAL) << "bind error";
                exit(BIND_ERROR);
            }
            LOG(LogLevel::INFO) << "bind sunccess";
        }
        void ListenOrDie(int backlog) override
        {
            int n = ::listen(_sockfd, backlog);
            if (n < 0)
            {
                LOG(LogLevel::FATAL) << "listen error";
                exit(LISTEN_ERROR);
            }
            LOG(LogLevel::INFO) << "listen sunccess";
        }
        std::shared_ptr<Socket> Accept(InetAddr* Client) override
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof(len);
            int fd = ::accept(_sockfd, CONV(peer), &len);
            if (fd < 0)
            {
                LOG(LogLevel::WARNING) << "accept warning ...";
                return nullptr; // TODO
            }
            Client->SetAddr(peer);// 解耦，让另一个对象来管理客户端的ip and 端口号信息
            return std::make_shared<TcpSocket>(fd);
        }

        bool Connect(std::string& ip, uint16_t port) override
        {
            InetAddr client(ip,port);//初始化客户端
            int n = ::connect(_sockfd,client.NetAddrptr(),client.NetAddrLen());
            if(n < 0)
            {
                LOG(LogLevel::FATAL) << "connnect error";
                return false;
            }
            return true;
        }
        
        
        int Recv(std::string* out) override
        {
            char buffer[1024];
            ssize_t n = ::recv(_sockfd, buffer, sizeof(buffer) - 1, 0);
            if(n > 0)
            {
                buffer[n] = 0;
                *out += buffer;
            }
            return n;
        }

        virtual int Send(const std::string& message) override
        {
            return send(_sockfd, message.c_str(), message.size(), 0);
        }

        void Close() override
        {
            if(_sockfd >= 0)
                ::close(_sockfd);
        }
        ~TcpSocket()
        {
        }

    private:
        int _sockfd;
    };
}